<div class="layui-fluid layui-layout-admin">
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		<legend class="title"></legend>
	</fieldset>

	<form class="layui-form" action="">
		<div class="layui-form-item">
			<label class="layui-form-label">banner位</label>
			<div class="layui-input-inline">
				<select name="banner_seat_id">

				</select>
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">banner图</label>
			<div class="layui-input-inline">
				<div class="layui-upload">
					<button type="button" class="layui-btn" id="banner_image_btn">上传图片</button>
					<div class="layui-upload-list">
						<img class="banner_image" id="banner_image">
						<p id="demoText"></p>
					</div>
				</div>
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">描述信息</label>
			<div class="layui-input-inline">
				<input type="text" name="desc" autocomplete="off" placeholder="请输入描述信息" class="layui-input" value="">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<div class="layui-footer">
					<input type="hidden" name="id" value="0" />
					<input type="hidden" name="image" value="" />
					<input type="hidden" name="list_table_name" value="banner-image-list" />
					<button class="layui-btn" lay-submit lay-filter="LAY-send-submit">立即提交</button>
				</div>
			</div>
		</div>
	</form>
</div>

<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
</script>

<script>
	layui.data.sendParams = function(params) {
		console.log(params);
		layui.use(['form', 'upload'], function() {
			let $ = layui.jquery,
				form = layui.form,
				upload = layui.upload,
				router = layui.router();

			let _index = layer.load();
			$(".layui-form").attr('action', layui.setter.API_HOST + 'admin/banner/image_save')
			let _banner_image_id = params.id | 0;
			$(".title").text((_banner_image_id > 0 ? '编辑' : '添加') + 'banner图');

			if (_banner_image_id) {
				layui.admin.req({
					url: layui.setter.API_HOST + 'admin/banner/image_save',
					data: {
						id: _banner_image_id
					},
					done: function(res) {
						console.log(res);
						layer.close(_index);
						if (res.code == 0) {
							$("input[name='title']").val(res.data.title);
							$("input[name='image']").val(res.data.image);
							$("input[name='id']").val(res.data.id);
							$("input[name='desc']").val(res.data.desc);
							$(".banner_image").attr('src', res.data.min_image);

							get_banner_seat_list(res.data.banner_seat_id);
						} else {
							layer.msg('请求失败', {
								icon: 5
							})
						}
					}
				});
			} else {
				get_banner_seat_list(0);
			}

			function get_banner_seat_list(_banner_seat_id) {
				layui.admin.req({
					url: layui.setter.API_HOST + 'admin/banner/seat_list',
					done: function(res) {
						layer.close(_index);
						if (res.code == 0) {
							let _seat_html = [];
							_seat_html.push('<option>请选择</option>');
							$.each(res.data, function(i, d) {
								if (_banner_seat_id == d.id) {
									_seat_html.push('<option value="' + d.id +
										'" selected>' + d.title + '</option>');
								} else {
									_seat_html.push('<option value="' + d.id + '">' + d
										.title + '</option>');
								}
							});
							console.log(_seat_html)
							$("select[name='banner_seat_id']").html(_seat_html.join(''));
							form.render();
						}
					}
				});
			}

			//普通图片上传
			var uploadInst = upload.render({
				elem: '#banner_image_btn',
				headers: { //通过 request 头传递
					token: layui.data('layuiAdmin').token
				},
				url: layui.setter.API_HOST + 'admin/util/image_upload', //改成您自己的上传接口
				done: function(res) {
					//如果上传失败
					if (res.code > 0) {
						return layer.msg('上传失败');
					}
					//上传成功
					$(".banner_image").attr('src', res.data.image);
					$("input[name='image']").val(res.data.path);
				},
				error: function() {
					//演示失败状态，并实现重传
					var demoText = $('#demoText');
					demoText.html(
						'<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>'
						);
					demoText.find('.demo-reload').on('click', function() {
						uploadInst.upload();
					});
				}
			});
		});
	}
</script>
